﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace StationUpdate
{
    /// <summary>
    /// LocalTime 的摘要说明。
    /// </summary>
    public class LocalTime
    {
        public LocalTime()
        {

        }
        [DllImport("Kernel32.dll")]
        public static extern void GetLocalTime(SystemTime st);

        [DllImport("Kernel32.dll")]
        public static extern void SetLocalTime(ref SystemTime st);

        public struct SystemTime
        {
            public SystemTime(DateTime dt)
            {
                this.Year = (ushort)(dt.Year);
                this.Month = (ushort)(dt.Month);
                this.Day = (ushort)(dt.Day);
                this.Hour = (ushort)(dt.Hour);
                this.Minute = (ushort)(dt.Minute);
                this.Second = (ushort)(dt.Second);
                this.Millisecond = (ushort)(dt.Millisecond);
                this.DayOfWeek = (ushort)(dt.DayOfWeek);
            }
            public ushort Year;
            public ushort Month;
            public ushort DayOfWeek;
            public ushort Day;
            public ushort Hour;
            public ushort Minute;
            public ushort Second;
            public ushort Millisecond;
        }

        /// <summary>
        /// 设置本地时间
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static bool SetLocalTime(DateTime dt)
        {
            try
            {
                SystemTime st = new SystemTime(dt);
                SetLocalTime(ref st);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
